package com.game;

import java.awt.*;
import java.util.Random;

public class Bot extends Tank{
    //定义移动时间
    int moveTime = 0;

    public Bot(String img, int x, int y, GamePanel gamePanel, String upImg, String leftImg, String rightImg, String downImg) {
        super(img, x, y, gamePanel, upImg, leftImg, rightImg, downImg);
    }

    /*
    * 敌方坦克移动方法
    * */
    public void go() {
        attack();
        if (moveTime >= 20) {
            setDirection(getRandomDirection());
            moveTime = 0;
        } else {
            moveTime++;
        }
        switch (getDirection()) {
            case UP:
                upWard();
                break;
            case DOWN:
                downWard();
                break;
            case LEFT:
                leftWard();
                break;
            case RIGHT:
                rightWard();
                break;
        }
    }

    /*
    * 获取敌方坦克随机移动方向
    * */
    public Direction getRandomDirection(){
        Random random = new Random();
        int rnum = random.nextInt(4);
        switch (rnum){
            case 0:
                return Direction.UP;
            case 1:
                return Direction.DOWN;
            case 2:
                return Direction.LEFT;
            case 3:
                return Direction.RIGHT;
            default:
                return null;
        }
    }

    /*
    * 敌方坦克攻击
    * */
    @Override
    public void attack() {
        Point p = getHeadPoint();
        Random random = new Random();
        //设置敌方坦克攻击频率
        int rnum = random.nextInt(100);
        if (rnum < 2){
            getGamePanel().enemyBulletsList.add(new EnemyBullet("image/bullet/bulletYellow.gif",p.x-7,p.y-7,this.getGamePanel(),getDirection()));
        }
    }

    @Override
    public void paintSelf(Graphics g) {
        go();
        g.drawImage(getImg(),x,y,getWidth(),getHeight(),null);
    }

    @Override
    public Rectangle gerRec() {
        return new Rectangle(x,y,getWidth(),getHeight());
    }
}
